﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TOSCOMMCreate;


namespace TOSCOMMCreate
{
    public partial class FrmMain: Form
    {
        private ClsProtol clsProtol = new ClsProtol();
        public FrmMain()
        {
            InitializeComponent();
            this.Text = $"鼎信规约生成工具 {Assembly.GetEntryAssembly()?.GetName().Version}";
            this.tableLayoutPanel1.Dock = DockStyle.Fill;
            
            this.cBControl1.SelectedIndex = 0;
        }

        private void tB_data_TextChanged(object sender, EventArgs e)
        {
            clsProtol.setData(tB_data.Text);
            clsProtol.getLength();
        }


        private void btnCopy_Click(object sender, EventArgs e)
        {

            if (!string.IsNullOrEmpty(tB_viewer.Text))
            {
                Clipboard.SetText(tB_viewer.Text);
                if (Clipboard.GetText() == tB_viewer.Text) 
                { 
                    MessageBox.Show("拷贝成功！","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                }
            }
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            tB_viewer.Text = clsProtol.getString();
        }

        private void cBControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            setControl2ByControl1();
            byte control = (cBControl1.SelectedIndex == 0) ? (byte)0x00 : (byte)0x01;
            clsProtol.setControl(control, 1);
        }

        private void cBControl2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(cBControl2.SelectedIndex < 0)
            {
                return;
            }
            string hexString = cBControl2.SelectedItem.ToString().Split(' ')[0];
            byte control = Convert.ToByte(hexString, 16);
            clsProtol.setControl(control, 2);
        }

        public void setControl2ByControl1()
        {
            cBControl2.Items.Clear();
            if(cBControl1.SelectedIndex == -1)
            {
                return;
            }
            if (cBControl1.SelectedIndex == 0)
            {

                var dict = ClsEnumHelper.GetEnumDictionary<ClsProtol.Control2ValueInDataFrame>();
               
                foreach (KeyValuePair<int, string> item in dict)
                {
                    cBControl2.Items.Add($"0x{item.Key:X2} {item.Value}");
                }   
            }
            else if (cBControl1.SelectedIndex == 1)
            {
                var dict = ClsEnumHelper.GetEnumDictionary<ClsProtol.Control2ValueInControlFrame>();
                foreach (KeyValuePair<int, string> item in dict)
                {
                    cBControl2.Items.Add($"0x{item.Key:X2} {item.Value}");
                }
            }

            if (cBControl2.Items.Count > 0)
            {
                cBControl2.SelectedIndex = 0;
            }
            
        }

        private void btnCreateTimeStamp_Click(object sender, EventArgs e)
        {
            string now = ClsTimeStamp.getNowString();
            int curPos = tB_data.SelectionStart;
            tB_data.Text = tB_data.Text.Insert(curPos,now);
            tB_data.SelectionStart = curPos + now.Length;
        }
    }
}
